package com.shenjies88.practice.baseframework.config;

import com.shenjies88.practice.baseframework.tools.DateTimeTools;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.document.Document;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.List;

/**
 * @author by shenjies88
 * @since 2025/9/13 19:28
 */
@AllArgsConstructor
@Configuration
public class ChatClientConfig {

    @Resource(name = "qwen3ChatModel")
    private final OllamaChatModel qwen3ChatModel;

    @Resource(name = "dsr1ChatModel")
    private final OllamaChatModel dsr1ChatModel;

    @Resource(name = "dsllmChatModel")
    private final OllamaChatModel dsllmChatModel;

    private final ChatMemory chatMemory;

    @Resource(name = "jdbcChatMemory")
    private final ChatMemory jdbcChatMemory;

    private final VectorStore vectorStore;

    @Bean
    public ChatClient qwen3ChatClient() {
        return ChatClient
                .builder(qwen3ChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .build();
    }

    @Bean
    public ChatClient qwen3ToolChatClient() {
        ToolCallback[] toolCallback = ToolCallbacks.from(new DateTimeTools());
        return ChatClient
                .builder(qwen3ChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultToolCallbacks(toolCallback)
                .build();
    }

    @Bean
    public ChatClient dsr1ChatClient() {
        return ChatClient
                .builder(dsr1ChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .build();
    }

    @Primary
    @Bean
    public ChatClient dsllmChatClient() {
        return ChatClient
                .builder(dsllmChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .build();
    }

    @Bean
    public ChatClient dsllmRagChatClient() {
        List<Document> documents = List.of(
                new Document("我的猫的名字是亚米"));
        vectorStore.add(documents);

        return ChatClient
                .builder(dsllmChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor(), QuestionAnswerAdvisor.builder(vectorStore).build())
                .build();
    }

    @Bean
    public ChatClient dsllmMemoryChatClient() {
        return ChatClient
                .builder(dsllmChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor(), MessageChatMemoryAdvisor.builder(chatMemory).build())
                .build();
    }

    @Bean
    public ChatClient dsllmJdbcChatClient() {
        return ChatClient
                .builder(dsllmChatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor(), MessageChatMemoryAdvisor.builder(jdbcChatMemory).build())
                .build();
    }
}
